package com.tanhua.server.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.tanhua.model.enums.CommentSubject;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Visitors;
import com.tanhua.model.vo.MovementsVO;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVO;
import com.tanhua.server.service.MovementsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author Xiao
 * @date 2021/10/25
 */
@RestController
@RequestMapping("/movements")
public class MovementsController {
    @Autowired
    private MovementsService movementsService;

    /**
     * 圈子-动态-发布
     * @param movement
     * @return
     */
    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile imageContent[]) throws IOException {
        movementsService.movements(movement, imageContent);
        return ResponseEntity.ok("发布成功！");
    }

    /**
     * 圈子-我的动态
     * @param page
     * @param pagesize
     * @param userId
     * @return
     */
    @GetMapping("all")
    public ResponseEntity all(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pagesize,
                              Long userId) {
        PageResult pageResult = movementsService.all(page, pagesize, userId);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 圈子-好友动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity findFriendMovements(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementsService.findFriendMovements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 圈子-推荐动态
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementsService.recommend(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 圈子-单条动态
     * @param movementId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity findSingleMovement(@PathVariable("id") String movementId) {
        MovementsVO movementsVO = movementsService.findSingleMovement(movementId);
        return ResponseEntity.ok(movementsVO);
    }

    /**
     * 圈子-动态互动-(取消)点赞、(取消)喜欢
     * @param movementId
     * @param dispose
     * @return
     */
    @GetMapping("/{id}/{dispose}")
    public ResponseEntity dispose(
            @PathVariable("id") String movementId,
            @PathVariable("dispose") String dispose) {
        //choose：标志正向(如点赞)/反向(如取消点赞)操作
        Boolean choose = !(StringUtils.contains(dispose, "dis")
                || StringUtils.contains(dispose, "un"));
        CommentType commentType = CommentType.LIKE;
        if (StringUtils.contains(dispose, "love")) {
            commentType = CommentType.LOVE;
        }
        /*if (StringUtils.equals("like", dispose)) {
            choose = true;
            commentType = CommentType.LIKE;
        } else if (StringUtils.equals("dislike", dispose)) {
            commentType = CommentType.LIKE;
        } else if (StringUtils.equals("love", dispose)) {
            choose = true;
            commentType = CommentType.LOVE;
        } else {
            commentType = CommentType.LOVE;
        }*/
        Integer count = movementsService.disposeComment(movementId, choose, commentType, CommentSubject.MOVEMENT);
        return ResponseEntity.ok(count);
    }

    /**
     * 交友-谁看过我--查看最近五个用户
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors() {
        List<VisitorsVO> visitors = movementsService.visitors();
        return ResponseEntity.ok(visitors);
    }
}
